package com.realeyes.osmf.plugins
{
	import com.realeyes.osmf.elements.WatermarkProxyElement;
	import com.realeyes.osmf.events.DebugEvent;
	import com.realeyes.osmf.interfaces.IVideoShell;
	import com.realeyes.osmf.utils.PluginUtils;
	
	import flash.external.ExternalInterface;
	
	import org.osmf.events.MediaElementEvent;
	import org.osmf.events.TimeEvent;
	import org.osmf.media.MediaElement;
	import org.osmf.media.MediaFactoryItem;
	import org.osmf.media.MediaFactoryItemType;
	import org.osmf.media.MediaResourceBase;
	import org.osmf.media.PluginInfo;
	import org.osmf.net.NetLoader;
	import org.osmf.traits.MediaTraitType;
	import org.osmf.traits.TimeTrait;
	
	public class TrackerPluginInfo extends PluginInfo
	{
		// Plugin Namespace
		static public const NAMESPACE:String = "com.realeyes.osmf.plugins.TrackerPluginInfo";
		
		private var _vidShell:IVideoShell;
		private var _currentElement:MediaElement;
		
		
		///////////////////////////////////////////////////
		// CONSTRUCTOR
		///////////////////////////////////////////////////
		
		public function TrackerPluginInfo( mediaFactoryItems:Vector.<MediaFactoryItem>=null, mediaElementCreationNotificationFunction:Function=null )
		{
			
			//pass along the default Vector of MediaFacttoryItems, and specify which notification function to use for refference plugin implementation
			super( mediaFactoryItems, elementCreatedNotification );
		}
		
		
		/**
		 * Called from super class when plugin has been initialized with the MediaFactory from which it was loaded.
		 *  
		 * @param resource	Provides acces to the Resource used to load the plugin and any associated meta data
		 * 
		 */		
		
		override public function initializePlugin( resource:MediaResourceBase ):void
		{
			
			_vidShell = resource.getMetadataValue( PluginUtils.SHELL ) as IVideoShell;
			
			debug( "TrackerPluginInfo - Initialized" );
			
		}
		
		////////////////////////////////////////////////
		//REFFERENCE PLUGIN IMPLEMENTATION
		
		/**
		 *Called whenever a MediaElement is generated by the MediaFactory which the plugin was loaded from.
		 * This method will be called for any element that was created. It will be called for elements loaded before the plugin was loaded if they exist. 
		 * @param element
		 * 
		 */		
		protected function elementCreatedNotification( element:MediaElement ):void 
		{
			debug( "Element Created: " + element);
			
			_currentElement = element;
			
			
			//add event listeners for traits being added or removd for tracking purposes
			_currentElement.addEventListener( MediaElementEvent.TRAIT_ADD, _onAddTrait );
			_currentElement.addEventListener( MediaElementEvent.TRAIT_REMOVE, _onRemoveTrait );
		}
		////////////////////////////////////////////////
		
		
		
		
		
		protected function _onAddTrait( event:MediaElementEvent ):void 
		{
			debug("TRAIT ADDED: " + event.traitType);
			switch( event.traitType )
			{
				case MediaTraitType.LOAD:
				{
					debug( "Media is loadable" );
					break;
				}
				case MediaTraitType.PLAY:
				{
					debug( "Media is playable" );
					break;
				}
				case MediaTraitType.SEEK:
				{
					debug( "Media is seekable" );
					break;
				}
				case MediaTraitType.TIME:
				{
					//Get the time trait, so we can handle the duration changed event
					var timeTrait:TimeTrait = _currentElement.getTrait( MediaTraitType.TIME ) as TimeTrait;
					timeTrait.addEventListener( TimeEvent.DURATION_CHANGE, _onDurationChanged );
					
					debug( "Media has a timeline" );
					break;
				}
			}
		}
		
		protected function _onRemoveTrait( event:MediaElementEvent ):void 
		{
			debug( "Removing trait: " + event.traitType );
		}
		
		protected function _onDurationChanged( event:TimeEvent ):void 
		{
			debug( "New Duration: " + event.time );
		}
		
		// ==================================================
		// Helper methods
		// ==================================================
		
		protected function debug( msg:String ):void 
		{
			//trace( msg );
			_vidShell.debug( msg );
		}
	}
}